package com.excilys.formation.project.services.impl;

import java.util.List;
import java.util.Map;

import javax.validation.constraints.Min;

import org.joda.time.Interval;
import org.joda.time.YearMonth;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.excilys.formation.project.entities.Operation;
import com.excilys.formation.project.entities.TypeOperation;
import com.excilys.formation.project.repository.OperationDAO;
import com.excilys.formation.project.services.OperationService;

/**
 * 
 * @author excilys
 * 
 */
@Service
@Transactional(readOnly = true)
public class OperationServiceImpl implements OperationService {

	/**
	 * Instance de l'OperationDAO pour le service.
	 */
	@Autowired
	private OperationDAO operationDAO;

	@Override
	public List<Operation> getOperationsByCompteByYearMonth(String compteNumero, YearMonth mois) {
		return operationDAO.getOperationsByCompteAndTimeIntervalAndType(compteNumero, mois.toInterval(), null, 0, Integer.MAX_VALUE);
	}

	@Override
	public List<Operation> getOperationsByCompteByYearMonthAndType(String compteNumero, YearMonth mois, TypeOperation type, @Min(0) int offset,
			@Min(0) int limit) {
		return operationDAO.getOperationsByCompteAndTimeIntervalAndType(compteNumero, mois.toInterval(), type, offset, limit);
	}

	@Override
	public List<Operation> getOperationsByCompteByYearMonthExceptType(String compteNumero, YearMonth mois, TypeOperation type, @Min(0) int offset,
			@Min(0) int limit) {
		return operationDAO.getOperationsByCompteAndTimeIntervalExceptType(compteNumero, mois.toInterval(), type, offset, limit);
	}

	@Override
	public double getSumOperationsByCompteByYearMonthAndType(String compteNumero, YearMonth mois, TypeOperation type) {
		return operationDAO.getSumOperationsByCompteAndTimeIntervalAndType(compteNumero, mois.toInterval(), type);
	}

	@Override
	public Map<String, Double> getSumEncoursByClient(long clientId) {
		return operationDAO.getSumEncoursByClient(clientId);
	}

	@Override
	public TypeOperation getTypeByID(int i) {
		return operationDAO.getTypeByID(i);
	}

	@Override
	public long getCountOperationsByCompteAndTimeIntervalAndType(String compteNumero, Interval interval, TypeOperation type) {
		return operationDAO.getCountOperationsByCompteAndTimeIntervalAndType(compteNumero, interval, type);

	}

	@Override
	public long getCountOperationsByCompteAndTimeIntervalExceptType(String compteNumero, Interval interval, TypeOperation type) {
		return operationDAO.getCountOperationsByCompteAndTimeIntervalExceptType(compteNumero, interval, type);
	}
}
